package com.link.ppt.View;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.link.ppt.App.App;
import com.link.ppt.Base.BaseActivity;
import com.link.ppt.R;
import com.link.ppt.Utils.ViewHolderUtils;
import com.scrat.app.selectorlibrary.ImageSelector;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liucxu on 2018/1/31.
 */

public class SelectPhotoActivity extends BaseActivity {

    @BindView(R.id.back_layout)
    LinearLayout back_layout;
    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.remaining_photo_grid)
    GridView remaining_photo_grid;
    @BindView(R.id.right_layout)
    LinearLayout right_layout;
    @BindView(R.id.tv_next_step)
    TextView tv_next_step;

    private PhotoAdapter adapter;
    private List<String> paths=new ArrayList<>();

    private static final int REQUEST_CODE_SELECT_IMG = 1;
    private static final int MAX_SELECT_COUNT = 9;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ppt_select_photo;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        paths.add("");
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.GetInstance().finishActivity(SelectPhotoActivity.this);
            }
        });
        tv_title.setText("");
        adapter=new PhotoAdapter();
        remaining_photo_grid.setAdapter(adapter);
        right_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageSelector.show(SelectPhotoActivity.this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
            }
        });
        tv_next_step.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(paths.size()>1){
                    if (paths != null && paths.size() > 1) {
                        paths.remove(paths.size()-1);
                        String[] photos = (String[]) paths.toArray(new String[paths.size()]);
                        Intent intent_photo = new Intent(SelectPhotoActivity.this, PPTTitleKeywordActivity.class);
                        intent_photo.putExtra("isSelectPhoto", true);
                        intent_photo.putExtra("photos", photos);
                        startActivity(intent_photo);
                    }
                }
            }
        });
    }

    @Override
    public void setupData() {

    }

    class PhotoAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return paths.size();
        }

        @Override
        public Object getItem(int i) {
            return paths.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            if(convertView==null){
                convertView = View.inflate(SelectPhotoActivity.this, R.layout.adapter_ppt_select_photo, null);
            }
            initViews(i,convertView);
            return convertView;
        }

        private void initViews(final int position, View convertView){
            ImageView adapter_imv_card= ViewHolderUtils.get(convertView,R.id.adapter_imv_card);
            ImageView adapter_delete_photo=ViewHolderUtils.get(convertView,R.id.adapter_delete_photo);
            if(TextUtils.isEmpty(paths.get(position))){
                Glide
                        .with(SelectPhotoActivity.this)
                        .load(R.mipmap.add_ppt_icon)
                        .into(adapter_imv_card);
                adapter_delete_photo.setVisibility(View.INVISIBLE);
            }
            else{
                Glide
                        .with(SelectPhotoActivity.this)
                        .load(new File(paths.get(position)))
                        .into(adapter_imv_card);
                adapter_delete_photo.setVisibility(View.VISIBLE);
            }
            adapter_imv_card.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(TextUtils.isEmpty(paths.get(position))){
                        ImageSelector.show(SelectPhotoActivity.this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
                    }
                }
            });
            adapter_delete_photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    paths.remove(position);
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_SELECT_IMG) {
            List<String> paths_local = ImageSelector.getImagePaths(data);
            if (paths_local != null && paths_local.size() > 0) {
//                String[] photos = (String[]) paths.toArray(new String[paths.size()]);
//                Intent intent_photo = new Intent(SelectPhotoActivity.this, PPTTitleKeywordActivity.class);
//                intent_photo.putExtra("isSelectPhoto", true);
//                intent_photo.putExtra("photos", photos);
//                startActivity(intent_photo);
                paths.remove(paths.size()-1);
//                paths.clear();
                paths.addAll(paths_local);
                paths.add("");
                adapter.notifyDataSetChanged();
            }
        }
    }
}
